package com.huabo.system.service.impl;

import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.entity.TblManageUserBookMySql;
import com.huabo.system.mysql.mapper.TblManageUserBookMySqlMapper;
import com.huabo.system.oracle.entity.TblManageUserBook;
import com.huabo.system.oracle.mapper.TblManageUserBookMapper;
import com.huabo.system.service.TblManageUserBookservice;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.Set;

@Service
public class TblManageUserBookserviceImpl implements TblManageUserBookservice {


    @Resource
    private TblManageUserBookMapper tblManageUserBookMapper;

    @Resource
    private TblManageUserBookMySqlMapper tblManageUserBookMySqlMapper;


    @Override
    public void updateUserBook(Set<TblManageUserBook> userBookList, String orgid, String staffid) {
        staffid.substring(0, staffid.length() - 1);

        try {
            Integer count = 0;
            String sql = "";
            Iterator var6 = userBookList.iterator();

            while (var6.hasNext()) {
                TblManageUserBook manage = (TblManageUserBook) var6.next();
                //sql = "SELECT COUNT(0) FROM TBL_MANAGE_USER_BOOK where STAFFID = '" + manage.getStaffid() + "' AND BOOKID = '" + manage.getBookid() + "'";
                count = this.tblManageUserBookMapper.findCount(manage.getStaffid(), manage.getBookid());
                if (count == 0) {
                    this.tblManageUserBookMapper.saveEntity(manage);
                }
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }
    }

    @Override
    public void updateMySqlUserBook(Set<TblManageUserBookMySql> userBookMySqlList, String orgid, String staffid) {
        staffid.substring(0, staffid.length() - 1);

        try {
            Integer count = 0;
            String sql = "";
            Iterator var6 = userBookMySqlList.iterator();

            while (var6.hasNext()) {
                TblManageUserBookMySql manage = (TblManageUserBookMySql) var6.next();
                //sql = "SELECT COUNT(0) FROM TBL_MANAGE_USER_BOOK where STAFFID = '" + manage.getStaffid() + "' AND BOOKID = '" + manage.getBookid() + "'";
                count = this.tblManageUserBookMySqlMapper.findCount(manage.getStaffid(), manage.getBookid());
                if (count == 0) {
                    this.tblManageUserBookMySqlMapper.saveEntity(manage);
                }
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }
    }

    @Override
    public void delUserBook(String sid, String acctid) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            String[] staffs = sid.split(",");
            String[] acctids = acctid.split(",");

            for (int i = 0; i < acctids.length; ++i) {
                for (int j = 0; j < staffs.length; ++j) {
                    tblManageUserBookMapper.deleteSidAndAcctid(staffs[j], acctids[i]);
                }
            }
        } else {
            String[] staffs = sid.split(",");
            String[] acctids = acctid.split(",");

            for (int i = 0; i < acctids.length; ++i) {
                for (int j = 0; j < staffs.length; ++j) {
                    tblManageUserBookMySqlMapper.deleteSidAndAcctid(staffs[j], acctids[i]);
                }
            }
        }

    }

}
